﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.HeatingControlProject._2.数据层;
using thinger.HeatingControlProject._4.实体类;

namespace thinger.HeatingControlProject._1.界面层
{
    public partial class FrmParamSet : Form
    {
        #region 无边框拖动 

        private Point mPoint;
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        #endregion
        private string path = string.Empty;
        private PlcInfo plcInfo = null;
        private PlcInfoService infoService = new PlcInfoService();

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="plcinfo"></param>
        /// <param name="path"></param>
        public FrmParamSet(PlcInfo plcinfo,string path)
        {
            InitializeComponent();
            this.cmb_CPUType.DataSource = Enum.GetNames(typeof(CpuType));
            this.plcInfo = plcinfo;
            this.path = path;
            if (plcinfo != null)
            {
                this.txt_IPAddress.Text = this.plcInfo.IPAddress;
                this.cmb_CPUType.Text = this.plcInfo.CpuType.ToString();
                this.txt_Rack.Text = this.plcInfo.Rack.ToString();
                this.txt_Slot.Text = this.plcInfo.Slot.ToString();
            }

        }

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            if(this.plcInfo == null)
            {
                this.plcInfo= new PlcInfo();
            }
            this.plcInfo.IPAddress = this.txt_IPAddress.Text;
            this.plcInfo.CpuType = (CpuType)Enum.Parse(typeof(CpuType), this.cmb_CPUType.Text, true);
            this.plcInfo.Rack = Convert.ToInt16(this.txt_Rack.Text.Trim());
            this.plcInfo.Slot = Convert.ToInt16(this.txt_Slot.Text.Trim());

            try
            {
                infoService.SetPlcInfoToPath(this.plcInfo, this.path);
                this.DialogResult = DialogResult.OK;
                MessageBox.Show("PLC参数写入成功", "操作提示");
            }
            catch (Exception ex)
            {

                MessageBox.Show("PLC参数写入失败"+ ex.Message,"错误提示");
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult= DialogResult.Cancel;
        }
    }
}
